﻿//-----------------------------------------------------------------------
// <copyright file="SQLiteDataParameterCollection.cs" company="NextLine Software">
//     Copyright (c) NextLine Software. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace NextLine.Data.SQLite
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Data;

    public class SQLiteDataParameterCollection : List<IDataParameter>, IDataParameterCollection
    {
        public bool IsReadOnly
        {
            get
            {
                return false;
            }
        }

        public object this[string parameterName]
        {
            get
            {
                int index = this.IndexOf(parameterName);
                if (index >= 0)
                {
                    return this[index].Value;
                }

                throw new IndexOutOfRangeException("Parameter not found in collection: " + parameterName);
            }

            set
            {
                int index = this.IndexOf(parameterName);
                if (index >= 0)
                {
                    this[index].Value = value;
                }
                else
                {
                    throw new IndexOutOfRangeException("Parameter not found in collection: " + parameterName);
                }
            }
        }

        public bool Contains(string parameterName)
        {
            return this.IndexOf(parameterName) >= 0;
        }

        public int IndexOf(string parameterName)
        {
            for (int i = 0; i < this.Count; i++)
            {
                if (StringComparer.OrdinalIgnoreCase.Equals(parameterName, this[i].ParameterName))
                {
                    return i;
                }
            }

            return -1;
        }

        public void RemoveAt(string parameterName)
        {
            int index = this.IndexOf(parameterName);
            if (index >= 0)
            {
                this.RemoveAt(index);
            }
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }
    }
}
